import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";

var _dec, _dec2, _dec3, _class;

var _excluded = ["form", "fixed", "record"],
    _excluded2 = ["onDataChange"],
    _excluded3 = ["onRow"];
import React from 'react';
import { Form } from 'antd';
import Table from 'cpaas-ui/lib/table/index';
import { Bind } from 'lodash-decorators';
import styles from "./index.less?modules";
var TableRowContext = Table.TableRowContext;

var EditableRow = function EditableRow(_ref) {
  var form = _ref.form,
      fixed = _ref.fixed,
      record = _ref.record,
      props = _objectWithoutProperties(_ref, _excluded);

  if (!fixed && (record._status === 'create' || record._status === 'update')) {
    // eslint-disable-next-line
    record.$form = form;
    return /*#__PURE__*/React.createElement(TableRowContext.Provider, {
      value: form
    }, /*#__PURE__*/React.createElement("tr", props));
  }

  return /*#__PURE__*/React.createElement("tr", props);
};

var EditableFormRow = Form.create({
  fieldNameProp: null,
  // 表单值 改变 出发 onDataChange
  onValuesChange: function onValuesChange(props, changeValues, allValues) {
    var onDataChange = props.onDataChange,
        record = props.record;

    if (onDataChange) {
      onDataChange(record, changeValues, allValues);
    }
  },
  // 将 onDataChange 解构出来, 不应该传到 Form 下
  mapProps: function mapProps(props) {
    var _ref2 = props || {},
        onDataChange = _ref2.onDataChange,
        passProps = _objectWithoutProperties(_ref2, _excluded2);

    return passProps;
  }
})(EditableRow);
/**
 * 行内编辑表格 EditTable
 *
 * @extends {Component} - React.PureComponent
 * @reactProps {!boolean} [editing] - 新增、编辑的行数据需要提供该标识
 * @reactProps {!boolean} [isCreate] - 新增的行数据需要提供该标识
 * @reactProps {any} [otherProps] - 表格其他属性
 * @returns React.element
 * @example
 * 使用方式跟 antd Table 无异，唯一的区别是被编辑的行数据的record将会被添加$form属性。
 */

var EditTable = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(EditTable, _React$Component);

  var _super = _createSuper(EditTable);

  function EditTable() {
    var _this;

    _classCallCheck(this, EditTable);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.dataHasChange = false;
    return _this;
  }

  _createClass(EditTable, [{
    key: "componentDidUpdate",
    value:
    /**
     * 如果 dataSource 的 引用 发生了变化, 则重置 dataHasChange
     */
    function componentDidUpdate(prevProps) {
      var prevDataSource = prevProps.prevDataSource;
      var dataSource = this.props.dataSource;

      if (prevDataSource !== dataSource) {
        if (this.dataHasChange) {
          this.dataHasChange = false;
        }
      }
    }
  }, {
    key: "render",
    value: function render() {
      // antd table property
      var _this$props = this.props,
          onRow = _this$props.onRow,
          otherProps = _objectWithoutProperties(_this$props, _excluded3);

      var components = {
        body: {
          row: EditableFormRow
        }
      };

      var editTableProps = _objectSpread({
        components: components,
        rowClassName: styles['cpaas-edit-table'],
        onRow: this.onRow
      }, otherProps);

      return /*#__PURE__*/React.createElement(Table, editTableProps);
    }
    /**
     * 表单变化触发的回调
     * @param {object} record - 有变化的 行
     * @param {object} changeValues - 行 表单 改变的值
     * @param {object} allValues - 所有的表单值
     */

  }, {
    key: "handleDataChange",
    value: function handleDataChange(record, changeValues, allValues) {
      if (!this.dataHasChange) {
        this.dataHasChange = true;
      }

      var onDataChange = this.props.onDataChange;

      if (onDataChange) {
        onDataChange(record, changeValues, allValues);
      }
    }
    /**
     * 设置 antd Table onRow
     */

  }, {
    key: "onRow",
    value: function onRow(record, index) {
      var onRow = this.props.onRow;
      var userRowProps;

      if (onRow) {
        userRowProps = onRow(record, index);
      } else {
        userRowProps = {};
      }

      return _objectSpread(_objectSpread({
        record: record
      }, userRowProps), {}, {
        onDataChange: this.handleDataChange
      });
    }
    /**
     * 数据是否改变过
     * 给获得 ref 的 使用使用
     */

  }, {
    key: "hasDataChange",
    value: function hasDataChange() {
      return this.dataHasChange;
    }
  }]);

  return EditTable;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "handleDataChange", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleDataChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onRow", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "onRow"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "hasDataChange", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "hasDataChange"), _class.prototype)), _class));
export { EditTable as default };